package pacman.brains;

import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Vector2f;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.util.pathfinding.Path;
import org.newdawn.slick.util.pathfinding.Path.Step;

import pacman.actors.Brain;
import pacman.actors.Player;
import pacman.map.Map;

public class RedGhostBrain implements Brain {
    private static final int THINKINGTIME = 50;
    private static final int UPDATETIME = 5000;
    private int updateThinkingTime;
    private int updatePlayerPositionTime;
    private int currentStepIndex;
    private Path path;
    private Image dot;
    private Vector2f current;
    private Map map;
    private boolean cannotFindPath;
    
    public RedGhostBrain(Map map, Vector2f start) throws SlickException {
		this.map = map;
		this.current = start;
		init();
    }

    public void init() {
		path = null;
		updateThinkingTime = 0;
		updatePlayerPositionTime = 0;
		currentStepIndex = 0;
		cannotFindPath = false;
	
		try {
		    dot = new Image("data/dot.gif");
		 } catch (Exception e) {
			 System.out.println(e.getMessage());
		 }
		updatePathToPlayer();
    }
    
    public void render(BasicGameState game, Graphics g) {
		if (path != null) {
		    for (int i = 0; i < path.getLength(); i++) {
		    	Step a = path.getStep(i);
		    	dot.draw(a.getX() * map.getTileSize(), a.getY()* map.getTileSize());
		    }
		}
    }
    public void update(int delta) {
		if (path == null) {
		    updatePathToPlayer();
		    return;
		}
		updateThinkingTime = updateThinkingTime + delta;
		if (updateThinkingTime > THINKINGTIME) {
		    updateThinkingTime = 0;
		    if (currentStepIndex > path.getLength() - 1) {
		    	reThink(current, map, path);
		    }
		}
		updatePlayerPositionTime = updatePlayerPositionTime + delta;
	
		if (updatePlayerPositionTime > UPDATETIME) {
		    updatePlayerPositionTime = 0;
		    reThink(current, map, path);
		}

    }

    private void reThink(Vector2f current, Map map, Path path) {
		currentStepIndex = 0;
		path = null;
		updatePathToPlayer();
    }

    private void updatePathToPlayer() {
		Player pl = Map.getPlayer();
		try {
		    path = map.getUpdatedPath((int) current.getX() / map.getTileSize(),
			    (int) current.getY() / map.getTileSize(), (int) pl.getX()
				    / map.getTileSize(), (int) pl.getY()
				    / map.getTileSize());
		} catch (NullPointerException e) {
		    path = null;
		    cannotFindPath = true;
		}
    }
	
    public Step getCurrentStep() {
		if (path == null) {
		    updatePathToPlayer();
		}
		return path.getStep(currentStepIndex);
    }

    public void goToNextStep(Vector2f position) {
		this.currentStepIndex++;
		this.current = position;
    }

    public boolean isCannotFindPath() {
    	return cannotFindPath;
    }
    public void setCurrent(Vector2f current) {
    	this.current = current;
    }
}
